﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ajax.WebFactory
{
    /// <summary>
    /// 空数据处理类
    /// </summary>
    public class EmptyDataClass
    {
        static ITemplate T = null;
        static public void EmptyDataAction(Page _Page)
        {
            T = _Page.LoadTemplate("~/UserControl/UC_EmptyDataTemplate.ascx");
            Recursion(_Page);
        }
        static public void EmptyDataAction(Page _Page, string EmptyUC)
        {
            if (string.IsNullOrEmpty(EmptyUC))
            {
                EmptyDataAction(_Page);
            }
            else
            {
                T = _Page.LoadTemplate(EmptyUC);
                Recursion(_Page);
            }
        }

        #region 递归遍历页面的所有控件
        /// <summary>
        /// 递归遍历页面的所有控件
        /// </summary>
        /// <param name="c"></param>
        static private void Recursion(Control c)
        {

            foreach (Control C in c.Controls)
            {

                switch (C.GetType().Name)
                {
                    //PageLoad事件执行过以后，用户控件的Load事件才执行
                    //所以不对用户控件处理
                    case "UserControl":
                        continue;
                    case "Repeater":
                        #region Repeater
                        Repeater rp = (Repeater)C;

                        if (rp.Items.Count < 1)
                        {
                            List<string> list = new List<string>();
                            list.Add("");

                            rp.ItemTemplate = T;
                            rp.DataSource = list;
                            rp.DataBind();
                        }
                        #endregion
                        break;
                    case "DataList":
                        #region DataList
                        DataList dl = (DataList)C;

                        if (dl.Items.Count < 1)
                        {
                            List<string> list = new List<string>();
                            list.Add("");
                            //Dictionary<string, string> dic = new Dictionary<string, string>();
                            //foreach (DataKeyCollection dkc in dl.DataKeys)
                            //{
                            //    dic.Add(dkc.ToString(), dkc.ToString());

                            //} 
                            dl.ItemTemplate = T;
                            dl.DataSource = list;
                            dl.DataBind();
                        }

                        #endregion
                        break;
                    case "GridView":
                        #region GridView
                        //失败了
                        ////GridView gv = (GridView)C;
                        ////gv.EmptyDataTemplate = T;
                        //if (gv.Rows.Count < 1)
                        //{
                        //    List<string> list = new List<string>();
                        //    list.Add("");
                        //    gv.DataSource = list;
                        //    gv.DataBind();
                        //    int columnCount = gv.Rows[0].Cells.Count;
                        //    gv.Rows[0].Cells.Clear();
                        //    gv.Rows[0].Cells.Add(new TableCell());
                        //    gv.Rows[0].Cells[0].ColumnSpan = columnCount;
                        //    gv.Rows[0].Cells[0].Text = "无符合数据";
                        //    gv.ShowHeader = true;
                        //}
                        #endregion
                        break;
                }

                Recursion(C);
            }
        #endregion

        }
    }
}